<?php

# http://projects:8081/cdapp-hole-api/api/ip_query.php

include_once "_portal.php";

if (
  isset($_GET["output"]) &&
  $_GET["output"] == "yes") {
  output(ip_query());
}

function ip_query($ip = '') {
  $ip = $ip == '' ? get_client_ip() : $ip;
  $url = "http://ip-api.com/json/{$ip}?lang=zh-CN";
  $content = file_get_contents($url);
  $object = json_decode($content, true);
  return [
    'ip' => $ip,
    'country' => $object["country"],
    'region' => $object["regionName"],
    'city' => $object["city"]
  ];
}

# query example
# http://ip-api.com/json/221.198.64.142?lang=zh-CN

/*
{
"status": "success",
"country": "中国",
"countryCode": "CN",
"region": "TJ",
"regionName": "天津市",
"city": "天津",
"zip": "",
"lat": 39.1488,
"lon": 117.1762,
"timezone": "Asia/Shanghai",
"isp": "CNC Group CHINA169 Tianjin Province Network",
"org": "",
"as": "AS4837 CHINA UNICOM China169 Backbone",
"query": "221.198.64.142"
}
 */